package com.zsc.roadsys.controller;


import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;


/**
 * @author haoran
 */

@Controller
public class LoginController {
    @GetMapping("userLogin")
    public String login(){
        return "/login/login";
    }

    @PostMapping("userLogin")
    @ResponseBody
    public String userLogin(String name, String pwd) {
        Subject subject = SecurityUtils.getSubject();
        AuthenticationToken token = new UsernamePasswordToken(name, pwd);
        try {
            subject.login(token);
            return "登录成功";
//            session.setAttribute("user",token.getPrincipal().toString());
//            return "/admin/mainpage";

        } catch (AuthenticationException e) {
            e.printStackTrace();
            System.out.println("登录失败");
//            return "/comm/mainpage";
            return "登录失败";
        }

    }
}
